package com.tianyang.yukuaifuzs.business.main.presenter;

import com.alibaba.fastjson.JSONObject;
import com.tianyang.yukuaifuzs.business.main.bean.DeviceOutput;
import com.tianyang.yukuaifuzs.business.main.contract.CodePlateListContract;
import com.tianyang.yukuaifuzs.business.main.model.MainModel;
import com.tianyang.yukuaifuzs.utils.ToastUtil;

import java.util.List;

import rx.Observer;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;

/**
 * Description：码牌列表
 * Created by xyj on 2024/4/19
 */
public class CodePlateListPresenter implements CodePlateListContract.Presenter {

    private CodePlateListContract.View mView;

    private CompositeSubscription mSubscription;

    private String deviceType, userId;

    public CodePlateListPresenter(CodePlateListContract.View mView, String deviceType, String userId) {
        this.mView = mView;
        this.deviceType = deviceType;
        this.userId = userId;
        mSubscription = new CompositeSubscription();
    }

    @Override
    public void getDeviceList() {
        mView.showLoadingDialog(false);
        Subscription subscription = MainModel.getInstance().getDeviceList(deviceType, userId)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<List<DeviceOutput>>() {
                    @Override
                    public void onCompleted() {
                        mView.dismissLoadingDialog();
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.dismissLoadingDialog();
                        mView.showDeviceListView(null);
                    }

                    @Override
                    public void onNext(List<DeviceOutput> outputs) {
                        mView.showDeviceListView(outputs);
                    }
                });
        mSubscription.add(subscription);
    }

    @Override
    public void unRelationDevice(String id) {
        mView.showLoadingDialog(false);
        Subscription subscription = MainModel.getInstance().unRelationDevice(id, userId)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<JSONObject>() {
                    @Override
                    public void onCompleted() {
                        mView.dismissLoadingDialog();
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.dismissLoadingDialog();
                        mView.showErrorText(e);
                    }

                    @Override
                    public void onNext(JSONObject outputs) {
                        ToastUtil.show("解绑成功！");
                        getDeviceList();
                    }
                });
        mSubscription.add(subscription);
    }

    @Override
    public void relationDevice(String id, String bindPosNumber) {
        mView.showLoadingDialog(false);
        Subscription subscription = MainModel.getInstance().relationDevice(id, bindPosNumber, userId)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<JSONObject>() {
                    @Override
                    public void onCompleted() {
                        mView.dismissLoadingDialog();
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.dismissLoadingDialog();
                        mView.showErrorText(e);
                    }

                    @Override
                    public void onNext(JSONObject outputs) {
                        ToastUtil.show("关联音响成功！");
                        getDeviceList();
                    }
                });
        mSubscription.add(subscription);
    }

    @Override
    public void onDestroyPresenter() {
        if (mSubscription != null && !mSubscription.isUnsubscribed()) {
            mSubscription.unsubscribe();
            mSubscription = null;
        }
        if (mView != null) {
            mView = null;
        }
    }
}
